package com.ibuonline.b2b.shop.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.dimeng.common.base.SuperDAO;
import com.dimeng.common.util.StringUtil;
import com.ibuonline.b2b.entity.ShopBanner;
import com.ibuonline.b2b.entity.ShopBannerCategory;

public class ShopBannerDAO extends SuperDAO<ShopBanner> {

	private static final String DELETE_BANNER_CAT = "delete from ShopBannerCategory s where s.bannerId=?";
	@SuppressWarnings("unchecked")
	public List<ShopBanner> pagingShopBanner(final String sql, final Object[] fieldValues) {
		return this.getJdbcTemplate().query(sql, fieldValues, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				ShopBanner sb = new ShopBanner();
				sb.setId(rs.getInt("id"));
				sb.setName(rs.getString("name"));
				sb.setColor(rs.getString("color"));
				sb.setDisplayOrder(rs.getInt("display_order"));
				sb.setUseNum(rs.getInt("useNum"));
				return sb;
			}
		});
	}

	public int countRecord(String sql, Object[] params) {
		return jdbcTemplate.queryForInt(sql, params);
	}

	public int countRecord(String sql) {
		return jdbcTemplate.queryForInt(sql);
	}

	@SuppressWarnings("unchecked")
	public List<ShopBannerCategory> getAllShopCategory(String sql) {
		return jdbcTemplate.query(sql, new RowMapper() {
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				ShopBannerCategory stc = new ShopBannerCategory();
			    stc.setCatId(rs.getInt("cat_id"));
				return stc;
			}
    		
    	});
	}

	@SuppressWarnings("unchecked")
	public List<ShopBannerCategory> getAllShopBannerCategory() {
		return template.find("from ShopBannerCategory sbc order by sbc.catId, sbc.bannerId");
	}

	@SuppressWarnings("unchecked")
	public boolean saveShopBanner(final ShopBanner shopBanner) {
		 return (Boolean)template.execute(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Transaction transaction = null;
					try {
						transaction = session.beginTransaction();
						if(shopBanner.getId() != null) {
							session.saveOrUpdate(shopBanner);
						} else {
							session.save(shopBanner);
						}
						String catIds = shopBanner.getCatIds();
						if(StringUtil.notEmpty(catIds)) {
							String[] catId = catIds.split(",");
							for(int i=0; i<catId.length; i++) {
								ShopBannerCategory stc = new ShopBannerCategory();
								stc.setCatId(Integer.parseInt(catId[i].trim()));
								stc.setBannerId(shopBanner.getId());
								Query query = session.createQuery(DELETE_BANNER_CAT);
								query.setParameter(0,stc.getBannerId());
								query.executeUpdate();
								session.saveOrUpdate(stc);
							}
						}
						transaction.commit();
						return true;
					} catch (Exception e) {
						if (transaction != null)
							transaction.rollback();
						return false;
					}
				}
			});
	}

}
